package com.gexin.model.order;

import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.UUIDUtils;
import com.gexin.model.order.base.BaseOrderItem;
import com.gexin.plugin.sqlinxml.SqlKit;

import java.math.BigDecimal;
import java.util.List;

public class OrderItem
		extends BaseOrderItem<OrderItem>
{
	public static final OrderItem dao = new OrderItem();

	public List<OrderItem> getOrderItemsByOrderId(String order_id)
	{
		SQLUtils sql = new SQLUtils(SqlKit.sql("shoporder.getOrderItemsByOrderId"));
		return dao.find(sql.toString(), new Object[] { order_id });
	}

	public OrderItem getOrderItemByOrderId(String order_id)
	{
		return (OrderItem)dao.findFirst("select * from  t_order_item where order_id=?", new Object[] { order_id });
	}

	public List<OrderItem> findOrderItemByOrderId(String order_id)
	{
		return dao.find("select * from  t_order_item where order_id=?", new Object[] { order_id });
	}

	public OrderItem getItemByItemId(String itemId){
		String sql = "select * from t_member_order_integral where order_item_id=?";
		return findFirst(sql,new Object[]{itemId});
	}

	public List<OrderItem> getItembyId(String order_id)
	{
		String sql = " select i.*,p.`product_name`,p.`image_center` from t_order_item i left join t_products p on i.product_id=p.id where order_id=? ";
		return find(sql, new Object[] { order_id });
	}

	public OrderItem getOrderItemById(String item_id)
	{
		String sql = " select i.*,p.`product_name`,p.`image_center` from t_order_item i left join t_products p on i.product_id=p.id where item_id=? ";
		return (OrderItem)findFirst(sql, new Object[] { item_id });
	}

	public List<OrderItem> getProductsIdAndFreeAcquire() {
		String sql = "select  * from " +
				" t_free_acquire freeacquire join t_activity activity "+
				"on activity.id=freeacquire.activity_id and `order`.createtime between activity.activity_start_time and activity.activity_end_time and activity.type=1";
		return find(sql);
	}

	public List<OrderItem> getOrderItemsByProductIdAndFreeAcquire() {
		String sql = "select  item.item_id,item.unit_price,order.createtime" +
                ",order.user_id,item.product_id from t_order_item item " +
                "join t_free_acquire freeacquire on freeacquire.product_id=" +
                "item.product_id  join t_order `order` on `order`.id=" +
                "item.order_id join t_activity activity on activity.id=" +
                "freeacquire.activity_id and `order`.createtime between " +
                "activity.activity_start_time and activity.activity_end_time " +
                "and activity.type=1";
		return find(sql);
	}


	public List<OrderItem> findUserIdList(String userId)
	{
		SQLUtils sql = new SQLUtils(" select o.* ,i.item_id,s.state_name  from t_order o ");
		sql.append(" left join t_order_item i on i.order_id=o.id ");
		sql.append(" left join t_order_state s on o.state=s.state_id where true ");
		if (userId != null) {
			sql.whereLike("o.user_id", userId);
		}
		sql.append("  group by o.id order by o.createtime desc");
		return dao.find(sql.toString());
	}


	public void setOrderItem(Order order,Integer productId,int num){
		this.setOrderId(order.getId());
		this.setProductId(productId);
		this.setItemId(UUIDUtils.getUUIDInOrderId());
		this.setNums(num);
		this.setJifen(0);
		this.setSecondMoney(BigDecimal.ZERO);
		this.setFirstDiscountMoney(BigDecimal.ZERO);
		this.setIsGifts(0);
		this.setJifen(0);
	}
}
